package com.sqnu.dashe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqnu.dashe.common.R;
import com.sqnu.dashe.dto.DishDto;
import com.sqnu.dashe.dto.SetmealDto;
import com.sqnu.dashe.entity.Category;
import com.sqnu.dashe.entity.Dish;
import com.sqnu.dashe.entity.Setmeal;
import com.sqnu.dashe.entity.SetmealDish;
import com.sqnu.dashe.service.CategoryService;
import com.sqnu.dashe.service.SetmealDishService;
import com.sqnu.dashe.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author mmy
 * @create 2023-06-12 11:15
 * @description
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealDishController {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> saveSetmealDto(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmeal(setmealDto);
        return R.success("添加套餐成功");
    }
    @PutMapping
    public R<String> updateWithDish(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改套餐成功");
    }

    /**
     * 套餐分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //创建分页构造器
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        Page<SetmealDto> page1 = new Page<>();
        //条件构造器，添加分页条件，进行排序
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        queryWrapper.like(name != null, Setmeal::getName, name);
        //进行分页查询
        setmealService.page(setmealPage, queryWrapper);

        BeanUtils.copyProperties(setmealPage,page1,"records");
        List<SetmealDto> collect = setmealPage.getRecords().stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);//得到Category对象
            if(category!=null){
                String name1 = category.getName();
                setmealDto.setCategoryName(name1);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        page1.setRecords(collect);
        return R.success(page1);
    }

    /**
     * 修改套餐信息-根据id回显套餐信息
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getSetmealId(@PathVariable long id){
        SetmealDto withId = setmealService.getWithId(id);
        return R.success(withId);
    }
    /**
     * 删除套餐
     */
    @DeleteMapping
    public R<String> deleteSetmeal(@RequestParam List<Long> ids){
        setmealService.myDeleteService(ids);
        return R.success("删除套餐成功");
    }
    /**
     * 启售套餐
     */
    @PostMapping("/status/{val}")
    public R<String> star(Long ids,@PathVariable int val){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getId, ids);
        List<Setmeal> list = setmealService.list(queryWrapper).stream().map(item->{
            item.setStatus(val);
            return item;
        }).collect(Collectors.toList());
        setmealService.updateBatchById(list);
        return R.success("成功");
    }
    /**
     * 套餐分类
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        //查询套餐setmeal信息
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        queryWrapper.orderByDesc(Setmeal::getCreateTime);
        queryWrapper.eq(Setmeal::getStatus, 1);
        List<Setmeal> list = setmealService.list(queryWrapper);

        //查询套餐菜品关系
        return R.success(list);
    }
}
